﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fis.Web.Rules.Book
{
    public class DynamicRule : Agile.Engine.Data.DataRule
    {
        protected string BookID { get; set; }
        protected string BookYear { get; set; }
        protected string UserSid { get; set; }
        protected string UserName { get; set; }
        protected bool IsAdmin { get; set; }
        protected string BookRange { get; set; }
        protected string Version { get; set; }

        protected override bool BeforeExecute()
        {
            Message = string.Empty;

            CommonRule rule = new CommonRule();
            BookID = Paramater["SBOOKID"].ToString();
            bool result = true;

            //是否主管
            if (!Paramater.ContainsKey("IsAdmin"))
            {
                IsAdmin = rule.IsCustomAdmin(BookID
                    , Paramater["FUSERNAME"].ToString().Trim()
                    , Paramater["FPASSWORD"].ToString().Trim());
            }
            else
            {
                IsAdmin = (bool)Paramater["IsAdmin"];
            }
            Result["IsAdmin"] = IsAdmin;
            //账套年度
            BookYear = (Paramater.ContainsKey("YEAR") && Paramater["YEAR"] != null)
            ? Paramater["YEAR"].ToString() : rule.NullYear(BookID);
            //用户ID
            UserSid = (Paramater.ContainsKey("USERSID") && Paramater["USERSID"] != null)
            ? Paramater["USERSID"].ToString() : string.Empty;
            //用户名称
            UserName = (Paramater.ContainsKey("FUSERNAME") && Paramater["FUSERNAME"] != null)
            ? Paramater["FUSERNAME"].ToString() : string.Empty;
            //期间
            BookRange = (Paramater.ContainsKey("RANGE") && Paramater["RANGE"] != null)
            ? Paramater["RANGE"].ToString() : string.Empty;

            //期间
            Version = (Paramater.ContainsKey("Version") && Paramater["Version"] != null)
            ? Paramater["Version"].ToString() : "1.0.0.0";

            //是否包含默认账套
            if (!rule.HasDefaultBook(BookID))
            {
                Result["nobook"] = 1;
                Message = "帐套不存在，请使用主管身份登录创建帐套";
                Status = (IsAdmin) ? 1 : -1;
                return false;
            }            
            else
            {
                //重置链接
                rule.SwitchConnection(Model.DataContext, BookID, BookYear);
            }

            if (!rule.NullYear(BookID).Equals(BookYear))//缓存账套与服务器默认账套不一致，返回新的缓存账套
            {
                Result["cache"] = Agile.Engine.Utils.ZipUtils.ZipBase64(
                    Agile.Engine.Utils.IOUtils.ReadStream(rule.BookRealPath(BookID, rule.NullYear(BookID))));
                Status = -2;
                Message = "正在更新账套信息，请重新登录后再进行操作";
                return false;
            }

            var msg = string.Empty;
            if (WebContext.IsBookLocked(BookID, ref msg))
            {
                Message = msg;
                return false;
            }

            return result;
        }
    }
}
